package at.spengergasse.dao.derby;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

import at.spengergasse.dao.KundenDao;
import at.spengergasse.dao.util.JDBCConnectionController;
import at.spengergasse.model.Kunden;
import at.spengergasse.model.Plz;

public class KundenDaoDerbyImplTest
{
	private static Connection connection;
	private static KundenDao dao;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		connection = JDBCConnectionController.getConnection();
		// connection = ConnectionPoolController.getConnection();

		dao = new KundenDaoDerbyImpl();
	}

	@Test
	public void testSaveNew()
	{
		Kunden newKunde = new Kunden("leo@speng.at", "Herr", "Josef",
				"abcdefg", "Spengergasse 20");
		Plz plz = new Plz(1050, "Wien");
		plz.setId(Long.valueOf(1));
		newKunde.setPlz(plz);
		int numberOfRecords = dao.save(connection, newKunde);
		assertEquals(1, numberOfRecords);
		assertNotNull(newKunde.getId());
		assertNotNull(newKunde.getLastModifiedTS());
		List<Kunden> kunde = dao.findAll(connection);
		// TODO syso auskommentieren
		System.out.println(kunde.toString());
		assertEquals(4, kunde.size());
	}

	@Test
	public void testSaveExisting()
	{
		Kunden existingKunden = dao.findById(connection, Long.valueOf(1));
		existingKunden.setAnrede("Frau");
		existingKunden.setEmail("leone@speng.at");
		existingKunden.setName("Josefine");
		existingKunden.setStrasse("Spengergasse 27");
		Date lastModTS = existingKunden.getLastModifiedTS();
		dao.save(connection, existingKunden);
		assertFalse(lastModTS.equals(existingKunden.getLastModifiedTS()));
		Kunden checkKunde = dao.findById(connection, Long.valueOf(1));
		assertEquals("Frau", checkKunde.getANREDE());
		assertEquals("Josefine", checkKunde.getNAME());
		assertEquals("leone@speng.at", checkKunde.getEMAIL());
		assertEquals("Spengergasse 27", checkKunde.getSTRASSE());
	}

	@Test
	public void testDelete()
	{
		Kunden kunde = dao.findById(connection, Long.valueOf(2));
		dao.delete(connection, kunde);
		List<Kunden> kundeList = dao.findAll(connection);
		assertEquals(3, kundeList.size());
	}

	@Test
	public void testFindById()
	{
		Kunden kunde = dao.findById(connection, Long.valueOf(3));
		assertEquals("Seper", kunde.getNAME());
		assertEquals("Frau", kunde.getANREDE());
		assertEquals("sp@sp.at", kunde.getEMAIL());
		assertEquals("Reinergasse", kunde.getSTRASSE());
		assertEquals(Long.valueOf(1), kunde.getPlzId());
	}

	@Test
	public void testFindByIdWithNonExistingId()
	{
		Kunden kunde = dao.findById(connection, Long.valueOf(999));
		assertNull(kunde);
	}

	@Test
	public void testFindAll()
	{
		List<Kunden> kunde = dao.findAll(connection);
		assertEquals(3, kunde.size());
		// System.out.println(kunde.toString());
		assertEquals("Frau", kunde.get(0).getANREDE());
		assertEquals("Josefine", kunde.get(0).getNAME());
		assertEquals("leone@speng.at", kunde.get(0).getEMAIL());
		assertEquals("Spengergasse 27", kunde.get(0).getSTRASSE());
		assertEquals(Long.valueOf(1), kunde.get(0).getPlzId());
	}

}
